package model

import "time"

type SetMeal struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	StartTime time.Time
	EndTime time.Time
	SalesPrice float64
	Deposit float64
	ExclusiveCustomerList []*Customers `xorm:"-"`
	Remarks string
	SupportingProjectList []*SupportingProject `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Customers struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type SetMealCustomers struct {
	Id int64 `xorm:"pk autoincr"`
	SetMealId int64
	CustomersId int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type SupportingProject struct {
	Id int64 `xorm:"pk autoincr"`
	SetMealId int64
	Name string
	ProjectContent string
	FeedbackMode int
	MarketCapitalization float64
	RawMaterialList []*RawMaterial `xorm:"-"`
	PremiseUse bool
	CreatedAt time.Time
	UpdatedAt time.Time
}

type OriginalRawMaterial struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	CurrentStock int64
	NegativeInventory bool
	SafetyStock int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type RawMaterial struct {
	Id int64 `xorm:"pk autoincr"`
	SupportingProjectId int64
	MaterialNo string
	Name string
	Company string
	Specifications string
	MarketValue float64
	PackageQuantity int
	MarketCapitalization float64
	CreatedAt time.Time
	UpdatedAt time.Time
}

func CreateSetMeal(sm *SetMeal)(map[string]interface{}, error){

	x.Insert(sm)

	for _, ec := range sm.ExclusiveCustomerList{
		var smc = SetMealCustomers{
			SetMealId:   sm.Id,
			CustomersId: ec.Id,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		}
		x.Insert(smc)
	}

	for _, sp := range sm.SupportingProjectList{
		sp.SetMealId = sm.Id
		sp.CreatedAt = time.Now()
		sp.UpdatedAt = time.Now()
		x.Insert(sp)

		for _, rm := range sp.RawMaterialList{
			rm.SupportingProjectId = sp.Id
			rm.CreatedAt = time.Now()
			rm.UpdatedAt = time.Now()
			x.Insert(rm)
		}
	}

	return map[string]interface{}{"message":"成功","code":200}, nil
}

func QuerySetMeal(cId int64)([]*SetMeal, error){

	var smList []*SetMeal
	x.Join("INNER", "set_meal_customers", "set_meal_customers.set_meal_id = set_meal.id").
		Where("set_meal_customers.customers_id = ?", cId).
		And("start_time <= ?", time.Now()).
		And("end_timme >= ?", time.Now()).
		Find(&smList)

	for _, sm := range smList{

		x.Where("set_meal_id = ?", sm.Id).Find(&sm.SupportingProjectList)
		for _, sp := range sm.SupportingProjectList{
			x.Where("supporting_project_id = ?", sp.Id).Find(&sp.RawMaterialList)
		}
	}

	return smList, nil
}

func CheckOut(smList []*SetMeal)(map[string]interface{}, error){

	for _, sm := range smList{
		for _, sp := range sm.SupportingProjectList{
			if !sp.PremiseUse{
				return map[string]interface{}{"message":"失败","code":400}, nil
			}
		}
	}

	return map[string]interface{}{"message":"成功","code":200}, nil
}

func SelectOriginalRawMaterial()([]*OriginalRawMaterial, error){

	var ormList []*OriginalRawMaterial
	x.Where("current_stock > ?", 0).Find(&ormList)

	return ormList, nil
}



























